package com.ruoyi.module.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import io.netty.util.internal.ObjectUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.FundDetailsMapper;
import com.ruoyi.module.domain.FundDetails;
import com.ruoyi.module.service.IFundDetailsService;

/**
 * 资金明细Service业务层处理
 *
 * @author fubojin
 * @date 2025-01-10
 */
@Service
public class FundDetailsServiceImpl implements IFundDetailsService
{
    @Autowired
    private FundDetailsMapper fundDetailsMapper;

    /**
     * 查询资金明细
     *
     * @param id 资金明细主键
     * @return 资金明细
     */
    @Override
    public FundDetails selectFundDetailsById(Long id)
    {
        return fundDetailsMapper.selectFundDetailsById(id);
    }
    /**
     * 查询资金明细
     *
     * @param orderNo 订单单号
     * @return 资金明细
     */
    @Override
    public FundDetails selectFundDetailsByOrderNo(Integer fundType, String orderNo)
    {
        List<FundDetails>  fundDetails = fundDetailsMapper.selectFundDetailsByOrderNo(fundType, orderNo);
        if(ObjectUtils.isEmpty(fundDetails)) {
            return null;
        }
        return fundDetails.get(0);
    }

    @Override
    public int updateLoan(String orderNo) {
        List<FundDetails> fundDetailsList = fundDetailsMapper.selectFundDetailsByOrderNo(3, orderNo);
        if (!ObjectUtils.isEmpty(fundDetailsList)) {
            FundDetails fundDetails = fundDetailsList.get(0);
            fundDetails.setLoanType(2);
            fundDetailsMapper.updateFundDetails(fundDetails);//更改为已借款
            fundDetails.setId(null);
            fundDetails.setFundType(10);
            fundDetails.setLoanType(3);
            fundDetails.setLoanMoney(new BigDecimal(0));
            return fundDetailsMapper.insertFundDetails(fundDetails);
        }
        return 0;
    }

    /**
     * 查询资金明细列表
     *
     * @param fundDetails 资金明细
     * @return 资金明细
     */
    @Override
    public List<FundDetails> selectFundDetailsList(FundDetails fundDetails)
    {
        return fundDetailsMapper.selectFundDetailsList(fundDetails);
    }

    /**
     * 查询借款模块资金明细列表
     *
     * @param fundDetails 资金明细
     * @return 借款资金明细集合
     */
    @Override
    public List<FundDetails> selectFundDetailsLoanList(FundDetails fundDetails)
    {
        return fundDetailsMapper.selectFundDetailsLoanList(fundDetails);
    }

    /**
     * 新增资金明细
     *
     * @param fundDetails 资金明细
     * @return 结果
     */
    @Override
    public int insertFundDetails(FundDetails fundDetails)
    {
        fundDetails.setCreateTime(DateUtils.getNowDate());
        if (fundDetails.getUserId() == 1L){
            fundDetails.setType(1);
        }else {
            fundDetails.setType(2);
        }
        return fundDetailsMapper.insertFundDetails(fundDetails);
    }

    /**
     * 修改资金明细
     *
     * @param fundDetails 资金明细
     * @return 结果
     */
    @Override
    public int updateFundDetails(FundDetails fundDetails)
    {
        return fundDetailsMapper.updateFundDetails(fundDetails);
    }

    /**
     * 批量删除资金明细
     *
     * @param ids 需要删除的资金明细主键
     * @return 结果
     */
    @Override
    public int deleteFundDetailsByIds(Long[] ids)
    {
        return fundDetailsMapper.deleteFundDetailsByIds(ids);
    }

    /**
     * 删除资金明细信息
     *
     * @param id 资金明细主键
     * @return 结果
     */
    @Override
    public int deleteFundDetailsById(Long id)
    {
        return fundDetailsMapper.deleteFundDetailsById(id);
    }

    @Override
    public FundDetails selectFundDetailsByUserId(Long userId) {
        return fundDetailsMapper.selectFundDetailsByUserId(userId);
    }
}
